package com.ruoyi.web.controller.community;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.CmtPub;
import com.ruoyi.community.service.ICmtPubService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/community/pub")
public class CmtPubController extends BaseController {

    @Autowired
    private ICmtPubService iCmtPubService;

    /**
     * 获取事务列表信息
     */

    @PreAuthorize("@ss.hasPermi('community:pub:list')")
    @GetMapping("/list")
    public TableDataInfo list(CmtPub pub){
        startPage();
        List<CmtPub> list = iCmtPubService.selectPubList(pub);
        return getDataTable(list);
    }

    /**
     * 新增事务信息
     */
    @PreAuthorize("@ss.hasPermi('community:pub:add')")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CmtPub pub)
    {

        pub.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCmtPubService.insertPub(pub));
    }

    /**
     * 修改事务号
     */
    @PreAuthorize("@ss.hasPermi('community:pub:edit')")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CmtPub pub)
    {

        pub.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iCmtPubService.updatePub(pub));
    }

    /**
     * 根据事务编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('community:pub:query')")
    @GetMapping(value = "/{pubId}")
    public AjaxResult getInfo(@PathVariable Long pubId)
    {
        return AjaxResult.success(iCmtPubService.selectPubById(pubId));
    }

    /**
     * 删除事务
     */
    @PreAuthorize("@ss.hasPermi('community:pub:remove')")
    @Log(title = "事务管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{pubIds}")
    public AjaxResult remove(@PathVariable Long[] pubIds)
    {
        return toAjax(iCmtPubService.deletePubByIds(pubIds));
    }

    /**
     * 获取事务选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<CmtPub> pubs = iCmtPubService.selectPubAll();
        return AjaxResult.success(pubs);
    }




}
